import java.util.ArrayList;
import java.util.List;

abstract class Person{
    protected String name;
    public String getName(){
        return name;
    }
    public abstract void accept(Action visitor);
}
interface Action{
    void getManConclusion(Man ConcreteElementA);
    void getWomanConclusion(Woman ConcreteElementB);
}
class Man extends Person{
    public Man(String name) {
        super();//调用父类型的午餐构造函数可以省略
        super.name=name;
    }

    @Override
    public void accept(Action visitor) {
        visitor.getManConclusion(this);
    }
}
class Woman extends Person{
    public Woman(String name) {
        super();//调用父类型的午餐构造函数可以省略
        super.name=name;
    }
    @Override
    public void accept(Action visitor) {
        visitor.getWomanConclusion(this);
    }
}
class Success implements Action{
    @Override
    public void getManConclusion(Man ConcreteElementA) {
        System.out.println(ConcreteElementA.getName()+"成功时，背后多半有一个伟大的女人");
    }

    @Override
    public void getWomanConclusion(Woman ConcreteElementB) {
        System.out.println(ConcreteElementB.getName()+"成功时，背后多半有一个失败的男人");
    }
}
class Failure implements Action{
    @Override
    public void getManConclusion(Man ConcreteElementA) {
        System.out.println(ConcreteElementA.getName()+"失败时，闷头喝酒，谁也劝不动");
    }

    @Override
    public void getWomanConclusion(Woman ConcreteElementB) {
        System.out.println(ConcreteElementB.getName()+"失败时，眼泪汪汪，谁也不用劝");
    }
}
class ObjectStructure{
    private List<Person> elements=new ArrayList<>();
    public void attach(Person element){
        this.elements.add(element);
    }
    public void dettach(Person element){
        this.elements.remove(element);
    }
    public void display(Action visitor){
        for (Person e:elements){
            e.accept(visitor);
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Person e1=new Man("男人-小明");
        Person e2=new Woman("女人-小红");
//        创建一个数据结构对象
        ObjectStructure os=new ObjectStructure();
        os.attach(e1);
        os.attach(e2);
        Action visitor=new Success();
        os.display(visitor);
        visitor=new Failure();
        os.display(visitor);
    }
}
